

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import webpage.CreateDBConnection;
import webpage.SendMail;

/**
 * Servlet implementation class ForgotPassword
 */
@WebServlet("/ForgotPassword")
public class ForgotPassword extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ForgotPassword() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String loginEmail = request.getParameter("loginEmail");
		CreateDBConnection create = new CreateDBConnection();
		String result = create.searchColumn("password", loginEmail, "email_address");
		if(!result.equals("No matching results")){
			SendMail send= new SendMail();
			String text = "Dear User,"
					+ "\n\n your password is "+result;
			String sendResult = send.sendMail(loginEmail, text);
			if (sendResult.equals("Success")){
				sendResult="Password has been sent to "+loginEmail;
			}
			request.setAttribute("sendResult",sendResult);
			RequestDispatcher rd;
			rd= request.getRequestDispatcher("ForgotPassword.jsp");
			rd.forward(request,response);
		}
	}

}
